﻿using System;

namespace LoRel
{
    public static class Extenders {
        public static ChangeAdapter GetAdapter( this INotifies notify ) {
            return new ChangeAdapter( notify );
        }
    } 

    public class ChangeAdapter: INotifies, IUpdates {

        public ChangeAdapter( INotifies notifies ) {
            notifies.ChangePending += EnqueueUpdate;
        }

        public event ChangeHandler AdditionPending;
        public event ChangeHandler UpdatePending;
        public event ChangeHandler RemovalPending;
        public event ChangeHandler ClearPending;
        
        public event ChangeHandler ChangePending;

        public void EnqueueUpdate( ChangeNoticeArgs args ) {
            switch( args.ChangeType ) {
                case ChangeType.Add: 
                    AdditionPending( args ); 
                    break;
                case ChangeType.Remove:
                    RemovalPending( args );
                    break;
                case ChangeType.Update:
                    UpdatePending( args );
                    break;
                case ChangeType.Clear:
                    ClearPending( args );
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }

}
